package org.imdm.portal.loadchecker.telnet;

import java.util.ArrayList;

import org.imdm.portal.loadchecker.ui.LoadInfo;

public class ReadLinkTimeCommand extends Command {

	private ArrayList<String> month_to_int;
	public ReadLinkTimeCommand(TelnetClientAdvance telnet) {
		super(telnet);
		month_to_int = new ArrayList<String>();
		month_to_int.add("Jan");
		month_to_int.add("Feb");
		month_to_int.add("Mar");
		month_to_int.add("Apr");
		month_to_int.add("May");
		month_to_int.add("Jun");
		month_to_int.add("Jul");
		month_to_int.add("Aug");
		month_to_int.add("Sep");
		month_to_int.add("Oct");
		month_to_int.add("Nov");
		month_to_int.add("Dec");
	}

	public String execute() throws TelnetException {
		String linkTime = LoadInfo._UNKNOWN;
		StringBuffer _buf = new StringBuffer("");
		String cd = sendCommand("cd /opt/MagellanNMS");
		if ( cd.indexOf("No such file or directory") >= 0 )
			return linkTime;

		String svr_time = sendCommand("date +%Y%t%b%t%e%t%R");
		String file_info = sendCommand("ls -l lib");

		int fileyear = 0, filemonth = 0, fileday = 0, filehour = 0, filemin = 0;
		int svryear = 0, svrmonth = 0, svrday = 0, svrhour = 0, svrmin = 0;

		//parse server time to int.
		if (svr_time != null) {
			String[] infos = svr_time.split("\\s+");
			if (infos != null && infos.length > 3) {
				svryear = Integer.parseInt(infos[0]);
				svrmonth = month_to_int.indexOf(infos[1]) + 1;
				svrday = Integer.parseInt(infos[2]);
				String[] infos2 = infos[3].split(":");
				if (infos2 != null && infos2.length > 1) {
					svrhour = Integer.parseInt(infos2[0]);
					svrmin = Integer.parseInt(infos2[1]);
				}
			}
		}

		// parse file time to int
		if (file_info != null) {
			String[] infos = file_info.split("\\s+");
			if (infos != null && infos.length > 8) {
				filemonth = month_to_int.indexOf(infos[5]) + 1;
				fileday = Integer.parseInt(infos[6]);
				String[] infos2 = infos[7].split(":");
				if (infos2 != null && infos2.length > 1) {
					filehour = Integer.parseInt(infos2[0]);
					filemin = Integer.parseInt(infos2[1]);
					if (filemonth > svrmonth) //last year within 6 month, the date format is still Dec 30 13:42
						fileyear = svryear - 1;
					else
						fileyear = svryear;
				}
				else {
					fileyear = Integer.parseInt(infos[7]);
				}
				linkTime = infos[5]+" "+infos[6]+" "+infos[7];
			}
		}

		if ( svryear == 0 || svrmonth == 0 || svrday == 0 ) //cannot get server time.
			return linkTime;
		
		if ( fileyear == 0 || filemonth == 0 || fileday == 0 )//file time is invalid.
			return linkTime;

		int delta = svrmonth + 12*(svryear - fileyear) - filemonth;
		if ( delta > 0 ) {//load was linked at least one month ago
			if ( delta == 1 ) {
				_buf.append("last month");
			}
			else {
				_buf.append(delta);
				_buf.append(" months ago");
			}
		}
		else { //linked at current month, so, go further to day delta.
			delta = svrday - fileday;
			if ( delta > 1 ) {
				_buf.append(delta);
				_buf.append(" days ago");
			}
			else if ( delta == 1 ) {
				_buf.append("yesterday");
			}
			else { //delta == 0, linked at same day. go to handle hours.
				delta = svrhour - filehour;
				if ( delta > 1 ) {
					_buf.append(delta);
					_buf.append(" hours ago");
				}
				else if ( delta == 1 )
					_buf.append("last hour");
				else { //delta == 0, linked at same hour, goto handle minute.
					delta = svrmin - filemin;
					_buf.append(delta);
					_buf.append(delta>1?" minutes ago":" minute ago");
				}
			}
		}
		return _buf.toString();
	}
}
